package com.hw.one.dam2.controller;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.hw.one.core.controller.BaseController;
import com.hw.one.core.result.MsgResult;
import com.hw.one.dam2.service.IExamineTargetService;
import com.hw.one.ext.util.UserUtil;
import org.springframework.web.bind.annotation.RestController;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/03/22 09:56.
 * 空气质量目标控制层
 */
@RestController
@RequestMapping(value = "examineTarget")
public class ExamineTargetController extends BaseController{

    @Autowired
    private IExamineTargetService examineTargetService;
    /**
     * 查询空气质量目标年度目标
     * @author 李亚坤
     * @createtime 2017年3月22日10:02:44
     * @param request
     * @return
     */
    @RequestMapping(value = "findAirQualityTargetGoals", method = RequestMethod.GET)
    public MsgResult findAirQualityTargetGoals(HttpServletRequest request){
        try {

            Map<String, Object> map = new HashMap<String, Object>();
            Calendar now = Calendar.getInstance();
            //如果年份为空,则认为是当前年
            String year = StringUtils.isEmpty(request.getParameter("year")) ||
                    Integer.valueOf(request.getParameter("year")) > now.get(Calendar.YEAR) ?
                    String.valueOf(now.get(Calendar.YEAR)) : request.getParameter("year");
            //如果月份为空,则认为是当前月
            String month = StringUtils.isEmpty(request.getParameter("month")) ||
                    Integer.valueOf(request.getParameter("month")) > now.get(Calendar.MONTH) + 1 ?
                    String.valueOf(now.get(Calendar.MONTH) + 1) : request.getParameter("month");

            map.put("year", year);
            map.put("month", month);
            // 根据登录用户，查询用户所属行政区域
            Map<String, Object> region = UserUtil.getRegion();
            String userRegionCode = (String) region.get("regionCode");

            map.put("regionCode", userRegionCode);

            Object resultObj = examineTargetService.findAirQualityTargetGoals(map);

            return MsgResult.okResult(resultObj);
        }catch (Exception e) {
            e.printStackTrace();
            return MsgResult.errorResult("查询空气质量目标年度目标异常,后台异常");
        }
    }

    /**
     * 查询空气质量目标完成情况(包括优良天数,PM10, PM25)
     * @author 李亚坤
     * @createtime 2017年3月22日10:51:02
     * @param request
     * @return
     */
    @RequestMapping(value = "findAirQualityGoalCompletion", method = RequestMethod.GET)
    public MsgResult findAirQualityGoalCompletion(HttpServletRequest request) {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            Calendar now = Calendar.getInstance();
            //如果年份为空,则认为是当前年
            String year = StringUtils.isEmpty(request.getParameter("year")) ||
                    Integer.valueOf(request.getParameter("year")) > now.get(Calendar.YEAR) ?
                    String.valueOf(now.get(Calendar.YEAR)) : request.getParameter("year");
            //如果月份为空,则认为是当前月
            String month = StringUtils.isEmpty(request.getParameter("month")) ||
                    Integer.valueOf(request.getParameter("month")) > now.get(Calendar.MONTH) + 1 ?
                    String.valueOf(now.get(Calendar.MONTH) + 1) : request.getParameter("month");
            map.put("year", year);
            map.put("month", month);

            // 根据登录用户，查询用户所属行政区域
            Map<String, Object> region = UserUtil.getRegion();
            String userRegionCode = (String) region.get("regionCode");

            JSONObject dataObj = new JSONObject();
            if ("20".equals(region.get("regionType"))) { //市级

                map.put("regionCode", userRegionCode);
                Object resultObj = examineTargetService.findAirQualityGoalCompletionWithCity(map);
                dataObj.put("cityData", resultObj);
                dataObj.put("cityName", region.get("regionName"));

                dataObj.put("areaData", null);
                dataObj.put("areaName", null);
                return MsgResult.okResult(dataObj);
            }

            if ("22".equals(region.get("regionType"))) { //区级
                //如果是区级则先查询 市级的 显示的数据
                map.put("regionCode", region.get("parentCode"));
                Object resultObjCity = examineTargetService.findAirQualityGoalCompletionWithCity(map);

                //查询区级显示的数据
                map.put("regionCode", userRegionCode);
                Object resultObjArea = examineTargetService.findAirQualityGoalCompletionWithArea(map);
                //查询上级的名称
                Map<String, Object> regionParent = examineTargetService.findRegionInfoById((String) region.get("parentCode"));
                dataObj.put("cityData", resultObjCity);
                dataObj.put("cityName", regionParent.get("regionName"));
                dataObj.put("areaData", resultObjArea);
                dataObj.put("areaName", region.get("regionName"));

                return MsgResult.okResult(dataObj);
            }
            return MsgResult.okResult();
        }catch (Exception e) {
            e.printStackTrace();
            return MsgResult.errorResult("查询空气质量目标完成情况异常,后台异常");
        }
    }
}
